package butines.kraz.entity;

import java.awt.Graphics2D;

import butines.core.Sprite;
import butines.core.SpriteSheet;
import butines.core.game.EntityMoving;
import static butines.core.math.Geometry.*;
import butines.core.math.Vector2f;

public class Projectile extends EntityMoving {

	private Unit attacker;
	private Unit enemy;
	private Weapon weapon;

	private Vector2f origin;
	private Vector2f target;
	private int damage;
	private Sprite sprite;

	public void draw(Graphics2D g, float offsetX, float offsetY) {
		float xScreen = getPosition().x - offsetX;
		float yScreen = getPosition().y - offsetY;
		sprite.draw(g, xScreen, yScreen, getForward().theta());
//		image.getImage(0, 0).draw(g, xScreen, yScreen, getForward().theta());
	}

	public void update(float tpf) {

		sprite.update(tpf);
		
		Vector2f oldPosition = getPosition().copy();

		getVelocity().mult(getForward(), getMaxSpeed());

		getPosition().add(getVelocity());

		float dist = distToLineSegment(oldPosition, getPosition(), enemy.getPosition());

		if (enemy.getRadius() + getRadius() >= dist) {
			if (!enemy.isDied()) {
				weapon.damage(attacker, enemy);
			}
			setAlive(false);
		}

		if (Vector2f.distance(getPosition(), target) < 2) {
			setAlive(false);
		}

	}

	public Vector2f getOrigin() {
		return origin;
	}

	public void setOrigin(Vector2f origin) {
		this.origin = origin;
	}

	public Vector2f getTarget() {
		return target;
	}

	public void setTarget(Vector2f target) {
		this.target = target;
	}

	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

	public Sprite getSprite() {
		return sprite;
	}
	
	public void setSheet(SpriteSheet sheet) {
		sprite = new Sprite(sheet);
	}
	
	public SpriteSheet getSheet() {
		return sprite == null ? null : sprite.getSheet();
	}

	public Unit getAttacker() {
		return attacker;
	}

	public void setAttacker(Unit attacker) {
		this.attacker = attacker;
	}

	public Unit getEnemy() {
		return enemy;
	}

	public void setEnemy(Unit enemy) {
		this.enemy = enemy;
	}

	public Weapon getWeapon() {
		return weapon;
	}

	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
	}

}
